/* .gprc.gp sample file
 * 
 * R. J. Cano, Jul 8 2014
*/

/* The perimeter for an isosceles triangle circumscribed in a circumference of radius "R",
 * if the inner angle there bisected by symmetry is "t";
*/
perim(R,t)= 2*R*(sin(t)+2*cos(t/2));

/* Contains an approx of the factor for which perim(R,solution01*Pi) is approx the same than Pi*R;
*/
solution01=0.7304740904201258333878; /* :-) Let's try this with a symbolic inverter!!! */
/*
 * Note: solution01*Pi approx. looks like 13129'7.21" (Of course, this is in terms of arc measure)
*/ 

/*
 * Def.: piu(t); Desc.: "The anyReal2Pi transformation under successive t+=piu(t)";
 *
 * piu(t) is [-2,0,2] for k integer, t=(4*k+c)*Pi and c=[0,1,2];
*/
piu=t->2*cos(t/2)/(2*sin(t/2)-1);

/*
 * Contributed by: Bill Allombert. Numerically works faster.
*/
biu=t->2*cotan(t/2);

/*
 * Def.: recipiu(t); Desc.: "Reciprocal for piu(t)";
 * 
 * recipiu(t) apparently have zeros for j non-negative integer and t=(+/-1)*(5^j/3)*Pi;
*/
recipiu=t->tan(t/2)-1/(2*cos(t/2));

/* A tiny correction just in time. */
fixIndex(i,j)=if(!i,j,i);

/* Decimal comparison */
places=(x,y=Pi)->-1+abs(log(abs(x)-abs(y))/log(10))\1;

/* Help + Hints */
info(L=1)={my(myTxt=["(help/hint)\t#1: For PiApproxInfo(5,1) and 12 iter., at least set:\n(help/hint)\n(help/hint)\t\t\\p 85030600;"]);print(myTxt[fixIndex(L%#myTxt,#myTxt)])}

/*
* Source: http://en.wikipedia.org/w/index.php?title=Approximations_of_%CF%80&oldid=613022442

a=((23+4*sqrt(34))/2);
b=((19*sqrt(2)+7*sqrt(17))/2);
c=((429+304*sqrt(2)));
d=((627+442*sqrt(2))/2);
u=(((a+sqrt(a^2-1))^2)*((b+sqrt(b^2-1))^2)*(c+sqrt(c^2-1))*(d+sqrt(d^2-1)));

PiapproxDanielShanks160decimalplaces= log((2*u)^6+24)/sqrt(3502);

Or:

log((2*(((((23+4*sqrt(34))/2)+sqrt(((23+4*sqrt(34))/2)^2-1))^2)*((((19*sqrt(2)+7*sqrt(17))/2)+sqrt(((19*sqrt(2)+7*sqrt(17))/2)^2-1))^2)*(((429+304*sqrt(2)))+sqrt(((429+304*sqrt(2)))^2-1))*(((627+442*sqrt(2))/2)+sqrt(((627+442*sqrt(2))/2)^2-1))))^6+24)/sqrt(3502);

 *
*/

/* "a" iterations, starting with "b" decimal places, multiplicating by "c" each iter, giving "d" extra places each iter. */
upTo(a,b,c,d)=if(a==0,b,upTo(a-1,b,c,d)*c+d);

PiApproxInfo(n,m=1)={my(data=[[355/113,6],[(2143/22)^(1/4),8],[log(640320^3+744)/sqrt(163),30],[log(((5280)*(236674+30303*sqrt(61)))^3+744)/sqrt(427),52],[log((2*(((((23+4*sqrt(34))/2)+sqrt(((23+4*sqrt(34))/2)^2-1))^2)*((((19*sqrt(2)+7*sqrt(17))/2)+sqrt(((19*sqrt(2)+7*sqrt(17))/2)^2-1))^2)*(((429+304*sqrt(2)))+sqrt(((429+304*sqrt(2)))^2-1))*(((627+442*sqrt(2))/2)+sqrt(((627+442*sqrt(2))/2)^2-1))))^6+24)/sqrt(3502),160]]);data[fixIndex(n%#data,#data)][fixIndex(m%2,2)]};

iterEstimate(N,h)={my(ans=ceil(log((10^h)/PiApproxInfo(N,2))/log(3)));[ans,PiApproxInfo(N,2)*(3^ans),upTo(ans,PiApproxInfo(N,2),3,h)]}

/*(EOF)*/